<template>
  <!--答辩组学生录入成绩/详情-->
  <div class="edu-page-warp reply-stu-result">
    <el-card>
      <div class="edu-page-title">{{$t('graduationDesign.replyGroupInfo')}}</div>
      <div class="edu-opertion-bar">
        <el-form :model="replyGroupForm" label-position="right" label-suffix="：" label-width="120px">
          <el-row>
            <el-col :span="6">
              <el-form-item :label="$t('graduationDesign.replyGroup')">
                <span>{{replyGroupForm.defenseGroupName}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('graduationDesign.replyLocation')">
                <span>{{replyGroupForm.defenseLocation}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('graduationDesign.replyTime')">
                <span>{{replyGroupForm.defenseStartTime}}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-card>
    <el-card>
      <div class="edu-page-title">{{actionType === 'VIEW' ? $t('graduationDesign.resultInfo') : $t('graduationDesign.entryResult')}}</div>
      <edu-table ref="replyStuResultTable" :data="replyStuResultData" @page-change="handlePageChange" :total="queryForm.total_" :pageNum="queryForm.pageNum_" :selection="false">
        <edu-table-column :label="$t('graduationDesign.studentId')" prop="studentId" min-width="80"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.name')" prop="name" min-width="100"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.college')" prop="collegeIdI18n" min-width="90"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.major')" prop="subjectIdI18n" min-width="90"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.direction')" prop="direction" min-width="90"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.subject')" prop="projectName" min-width="100"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.resultEntry')" min-width="100">
          <template slot-scope="scope">
            <el-select v-model="scope.row.defenseResult" clearable @change="handleResultChange($event, scope.row)" v-if="actionType === 'ENTRY'">
              <el-option v-for="item in resultTypeOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <span v-if="actionType === 'VIEW'">{{defenseResultFormat(scope.row.defenseResult)}}</span>
          </template>
        </edu-table-column>
        <edu-table-column :label="$t('graduationDesign.reviewOpinion')" min-width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.comments" :placeholder="$t('graduationDesign.pleaseEnter')" @blur="handleOpinionChange($event, scope.row)" v-if="actionType === 'ENTRY'" maxlength="250"></el-input>
            <span v-else>{{scope.row.comments}}</span>
          </template>
        </edu-table-column>
        <edu-table-column :label="$t('graduationDesign.replyQuestion')" min-width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.defenseQuestion" :placeholder="$t('graduationDesign.pleaseEnter')" @blur="handleQuestionChange($event, scope.row)" v-if="actionType === 'ENTRY'" maxlength="250"></el-input>
            <span v-else>{{scope.row.defenseQuestion}}</span>
          </template>
        </edu-table-column>
      </edu-table>
      <div class="result-footer" v-if="actionType === 'ENTRY'">
        <el-row class="result-button">
          <el-col>
            <el-button @click="onCancel">{{$t('graduationDesign.cancel')}}</el-button>
            <el-button type="primary" @click="onSave">{{$t('graduationDesign.save')}}</el-button>
            <el-button type="primary" @click="onSubmit">{{$t('graduationDesign.submit')}}</el-button>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/assignReplyStudent";

export default {
  name: 'reviewRelease',
  props: {
    replyGroupForm: {
      type: Object,
      default: {}
    },
    actionType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      api: API,
      getReviewList: '/api/designservice/defenseGroup/list',
      //查询参数
      queryForm: {
        pageNum_: 1,
        pageSize_: 20,
        condition: ''
      },
      replyStuResultData: [],
      resultTypeOptions: [{
        label: '优',
        value: 0
      }, {
        label: '良',
        value: 1
      }, {
        label: '中',
        value: 2
      }, {
        label: '及格',
        value: 3
      }, {
        label: '不及格',
        value: 4
      }, {
        label: '无效',
        value: 5
      }],
      replyGroupForm: {},
      assessedResultMap: {},
      assessedResultList: [] // 已评定成绩数据
    };
  },
  created() { },
  mounted() {
    this.queryForm.condition = this.replyGroupForm.id;
    this.searchList();
  },
  methods: {
    searchList() {
      this.api.getResultDetail(this.queryForm).then(res => {
        // 存初始值
        this.replyStuResultData = this.resultFormat(res.data.list);
        this.queryForm.total_ = res.data.total_;
        this.queryForm.pageNum_ = res.data.pageNum_;
      }).catch();
    },
    // 成绩详情列表 数据格式化
    resultFormat(list) {
      let dataset = [];
      list.forEach(item => {
        // 已选的成绩列表需要保留
        this.assessedResultList.forEach(ele => {
          if (item.studentId === ele.studentId) {
            item.defenseResult = ele.defenseResult;
            item.comments = ele.comments;
            item.defenseQuestion = ele.defenseQuestion;
          }
        });
        dataset.push(item);
      });
      return dataset;
    },
    handlePageChange(val) {
      this.queryForm.pageSize_ = val.pageSize;
      this.queryForm.pageNum_ = val.pageNum;
      this.searchList();
    },
    // 成绩change
    handleResultChange(val, row) {
      this.saveResultList(row);
    },
    // 成绩过滤显示
    defenseResultFormat(val) {
      const resultItem = this.resultTypeOptions.find(item => item.value === val);
      return resultItem ? resultItem.label : "";
    },
    // 保存当前修改值，且保证不重复
    saveResultList(row) {
      this.assessedResultMap[row.studentId] = row;
      this.assessedResultList = [];
      for (let item in this.assessedResultMap) {
        this.assessedResultList.push(this.assessedResultMap[item]);
      }
    },
    // 评审意见chang
    handleOpinionChange(ele, row) {
      this.saveResultList(row);
    },
    // 提出疑问chang
    handleQuestionChange(ele, row) {
      this.saveResultList(row);
    },
    // 保存
    onSave() {
      const params = {
        isSubmit: false,
        defenseGroupId: this.replyGroupForm.id,
        studentDefenseResultList: this.assessedResultList
      };
      console.log(params);
      this.$emit("reply-result-save", params, this.$t('graduationDesign.save'));
    },
    // 提交
    onSubmit() {
      const params = {
        isSubmit: true,
        defenseGroupId: this.replyGroupForm.id,
        studentDefenseResultList: this.assessedResultList
      };
      // 列表无数据，不能提交
      if (this.$refs.replyStuResultTable.getTotal() === 0) {
        this.$eduAlert(this.$t('graduationDesign.listCannotSubmit'), this.$t('graduationDesign.prompt'));
        return;
      }
      console.log(params);
      this.$emit("reply-result-submit", params, this.$t('graduationDesign.submit'));
    },
    onCancel() {
      this.$emit("reply-result-cancel");
    }
  }
};
</script>
<style lang="scss" scoped>
.reply-stu-result {
    .result-footer {
        width: 100%;
        height: 60px;
        border-top: 1px solid #dcdfe6;
        margin-top: 20px;
        padding-top: 20px;
        .result-button {
            float: right;
        }
    }
}
</style>
